package cn.yunyichina.provider.pay.alipay.service.impl;

import cn.yunyichina.provider.framework.dubbo.service.PayService;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.pay.PaymentSettingResponse;
import cn.yunyichina.provider.pay.alipay.config.Alipay;
import cn.yunyichina.provider.pay.alipay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.alipay.entity.PublicQrcodeRequest;
import cn.yunyichina.provider.pay.alipay.entity.PublicQrcodeResponse;
import cn.yunyichina.provider.pay.alipay.service.BaseTradeService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayOpenPublicQrcodeCreateRequest;
import com.alipay.api.response.AlipayOpenPublicQrcodeCreateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 支付宝服务窗业务
 * Created by Joe.zhou on 2017/11/27.
 */
@Service
public class AlipayPublicServiceImpl  extends BaseTradeService{

    private static final Logger logger = LoggerFactory.getLogger(AlipayPublicServiceImpl.class);

    /**
     * 服务窗二维码生成
     * @param json
     * @interfaceName: yyt.alipay.alipay.public.qrcode.create
     * @return
     */
    public Response createAlipayPublicQrcode(String json){
        logger.info("AlipayPublicServiceImpl.createAlipayPublicQrcode.json -> {}", json);
        Response response = new Response();

        PublicQrcodeRequest request = JsonUtils.parseObject(json, PublicQrcodeRequest.class);

        //查询支付配置
        //根据appId获取密钥
        ResCommon<PaymentSettingResponse> paymentSettingResponseResCommon = PayService.getPayClient().queryPaymentSettingByAppId(request.getAppId());
        PaymentSettingResponse paymentSetting = paymentSettingResponseResCommon.getResult();

        AlipayClient alipayClient = new DefaultAlipayClient(
                this.getConfig(Alipay.ALIPAY_GATEWAY_OPENAPI),
                request.getAppId(),
                paymentSetting.getPrivateKey(),
                "json",
                "GBK",
                paymentSetting.getPublicKey());
        AlipayOpenPublicQrcodeCreateRequest alipayOpenPublicQrcodeCreateRequest = new AlipayOpenPublicQrcodeCreateRequest();
        alipayOpenPublicQrcodeCreateRequest.setBizContent("{" +
                "\"code_info\":{" +
                "\"scene\":{" + "\"scene_id\":\""+ request.getId() +"\"" +"}},"+
                "\"code_type\":\"PERM\"," +
                "\"show_logo\":\"Y\"" +
                "  }");
        AlipayOpenPublicQrcodeCreateResponse alipayOpenPublicQrcodeCreateResponse = null;
        try {
            alipayOpenPublicQrcodeCreateResponse = alipayClient.execute(alipayOpenPublicQrcodeCreateRequest);
            if(alipayOpenPublicQrcodeCreateResponse.isSuccess()){
                PublicQrcodeResponse publicQrcodeResponse = new PublicQrcodeResponse();
                publicQrcodeResponse.setQrCode(alipayOpenPublicQrcodeCreateResponse.getCodeImg());
                publicQrcodeResponse.setExpireSecond(alipayOpenPublicQrcodeCreateResponse.getExpireSecond());
                response.setResultCodeAndMessageAndResult(Response.SUCCESS_CODE , Response.SUCCESS_MESSAGE , JsonUtils.toJsonString(publicQrcodeResponse));
            } else {
               logger.info("AlipayPublicServiceImpl.createAlipayPublicQrcode  获取二维码失败：" + JsonUtils.toJsonString(alipayOpenPublicQrcodeCreateResponse.getParams()));
               response.setResultCodeAndMessage(ResponseEnum.ALIPAY_PAY_PUBLIC_QRCODE_FAIL.getCode() , ResponseEnum.ALIPAY_PAY_PUBLIC_QRCODE_FAIL.getMessage());
            }
        } catch (Exception e) {
            logger.error("AlipayPublicServiceImpl.createAlipayPublicQrcode", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.ALIPAY_PAY_PUBLIC_QRCODE_EXCEPTION.getCode(),
                    ResponseEnum.ALIPAY_PAY_PUBLIC_QRCODE_EXCEPTION.getMessage()
            );
        }
        return response;

    }
}
